package com.statistics.thread;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.statistics.bean.ElectricityWeek;
import com.statistics.bean.Water;
import com.statistics.cloudpower.dao.TblElectricityStatisticsWeekDAO;
import com.statistics.cloudpower.dao.TblWaterStatisticsWeekDao;
import com.statistics.cloudpower.entity.TblElectricityStatisticsWeek;
import com.statistics.cloudpower.entity.TblEquipment;
import com.statistics.cloudpower.entity.TblWaterStatisticsWeek;
import com.statistics.tools.Tools;
import com.statistics.yfdata.dao.DataElectricityRepository;
import com.statistics.yfdata.dao.DataWaterRepository;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author MeowParse
 * @date 2017/9/1.
 */
@Component
@Scope("prototype")
public class WaterStatisticsWeekThread extends Thread{

	private Logger logger;
	private String uuid;
	private List<TblEquipment> obj;
	private int fragment_number;
	private TblWaterStatisticsWeekDao tblWaterStatisticsWeekDao;
	private DataWaterRepository dataWaterRepository;

	public WaterStatisticsWeekThread(List<TblEquipment> obj,
	                                 TblWaterStatisticsWeekDao dao, String uuid, int fragment_number,
	                                 DataWaterRepository dataWaterRepository) {

		this.obj = obj;
		this.fragment_number = fragment_number;
		this.uuid = uuid;
		this.dataWaterRepository = dataWaterRepository;
		this.tblWaterStatisticsWeekDao = dao;
		this.logger = Logger.getLogger("水务周统计");
	}

	@Override
	public void run() {

		//根据读取的command进行相应运算
		try {
			DateFormat dataFormat = DateFormat.getDateInstance();
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String thread_start_time = simpleDateFormat2.format(new Date());

			String now = dataFormat.format(new Date());
			Date today = simpleDateFormat.parse(now);
			Long start_date;//前一天起始时间的时间戳
			Long end_date;//前一天结束时间的时间戳

			List<TblEquipment> tblEquipment = obj;
			List records = null;
			int equipment_number;

			//每周电量统计
			start_date = today.getTime() - 24 * 3600 * 1000 * 7;
			end_date = today.getTime();

			logger.info(start_date);
			logger.info(end_date);

			records = new ArrayList<TblElectricityStatisticsWeek>();

			if (records == null) {
				return;
			}

			//对这n条设备数据逐条进行查找相应的电量列表
			for (equipment_number = 0; equipment_number < tblEquipment.size(); equipment_number++) {
				JSONArray jsonArray = new JSONArray();

				if (tblEquipment.get(equipment_number) == null) {
					continue;
				}

				//取得头尾数据差值
				String mac = tblEquipment.get(equipment_number).getMac();

				List<Water> waters = dataWaterRepository.getSubtractionByWeek(mac, start_date, end_date);

				if (waters == null) {
					//没有这条设备记录
					continue;
				}

				for (Water water : waters) {

					TblWaterStatisticsWeek one_record = new TblWaterStatisticsWeek();
					one_record.setMac(mac);
					one_record.setOffset(water.getOffset());
					one_record.setTotalWater(water.getTotalWater());
					one_record.setStatisticsTime(new Timestamp(start_date));

					records.add(one_record);

					JSONObject jsonObject = Tools.getJsonWaterStr(mac,water.getOffset(),water.getTotalWater(),start_date);
					jsonArray.add(jsonObject);
				}
//                Tools.sendTo61(Constants.DIANBIAOWEEK,jsonArray.toJSONString());
			}

			tblWaterStatisticsWeekDao.save(records);

			records.clear();

			String thread_end_time = simpleDateFormat2.format(new Date());
			logger.info(uuid + " 第" + fragment_number + "分片 Count=" + equipment_number + " Start=" + thread_start_time + " End=" + thread_end_time);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
	}

}
